package me.shingohu.man.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.RxLifecycle;
import com.trello.rxlifecycle2.android.FragmentEvent;
import com.trello.rxlifecycle2.android.RxLifecycleAndroid;
import com.yatatsu.autobundle.AutoBundle;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;
import me.shingohu.man.R;

/**
 * Created by hujie on 17/4/14.
 *
 * * 继承自rxliferecycler https://github.com/trello/RxLifecycle
 */

abstract class BaseFragment extends me.yokeyword.fragmentation.SupportFragment implements LifecycleProvider<FragmentEvent> {

    private Unbinder mUnbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lifecycleSubject.onNext(FragmentEvent.CREATE);
        autoBindBundle(savedInstanceState);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (layoutId() != 0) {
            View view = inflater.inflate(layoutId(), container, false);
            return view;
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        bindView();
        super.onViewCreated(view, savedInstanceState);
        lifecycleSubject.onNext(FragmentEvent.CREATE_VIEW);
        registerEventBus();
    }



    protected abstract int layoutId();

    /**
     * 是否注册eventbus
     * @return
     */
    protected boolean userEventBus() {
        return false;
    }


    /**
     * 使用自动绑定
     * @return
     */
    protected boolean userAutoBindBundle() {
        return true;
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (userAutoBindBundle()) {
            AutoBundle.pack(this, outState);
        }
    }


    /**
     * 自动绑定
     * @param savedInstanceState
     */
    protected void autoBindBundle(Bundle savedInstanceState) {
        if (userAutoBindBundle()) {
            if (savedInstanceState != null) {
                AutoBundle.bind(this, savedInstanceState);
            } else {
                AutoBundle.bind(this);
            }
        }
    }


    /**
     * 订阅eventbus
     */
    protected void registerEventBus() {
        if (userEventBus()) {
            EventBus.getDefault().register(this);
        }
    }


    /**
     * 取消订阅eventbus
     */
    protected void unRegisterEventBus() {
        if (userEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }


    /**
     * 垃圾回收
     */
    protected void gc() {
        unBindView();
        unRegisterEventBus();
        mUnbinder = null;
    }

    @Override
    public void onDestroy() {
        lifecycleSubject.onNext(FragmentEvent.DESTROY);
        super.onDestroy();
        gc();
    }

    /**
     * 绑定控件
     */
    protected void bindView() {
        if (userBindView()) {
            mUnbinder = ButterKnife.bind(this, getView());
        }
    }

    /**
     * 解绑控件
     */
    protected void unBindView() {
        if (userBindView() && mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

    /**
     * 是否使用butterknife
     * @return
     */
    protected boolean userBindView() {
        return true;
    }


    private final BehaviorSubject<FragmentEvent> lifecycleSubject = BehaviorSubject.create();

    @Override
    @NonNull
    @CheckResult
    public final Observable<FragmentEvent> lifecycle() {
        return lifecycleSubject.hide();
    }

    @Override
    @NonNull
    @CheckResult
    public final <T> LifecycleTransformer<T> bindUntilEvent(@NonNull FragmentEvent event) {
        return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
    }

    @Override
    @NonNull
    @CheckResult
    public final <T> LifecycleTransformer<T> bindToLifecycle() {
        return RxLifecycleAndroid.bindFragment(lifecycleSubject);
    }

    @Override
    public void onAttach(android.app.Activity activity) {
        super.onAttach(activity);
        lifecycleSubject.onNext(FragmentEvent.ATTACH);
    }


    @Override
    public void onStart() {
        super.onStart();
        lifecycleSubject.onNext(FragmentEvent.START);
    }

    @Override
    public void onResume() {
        super.onResume();
        lifecycleSubject.onNext(FragmentEvent.RESUME);
    }

    @Override
    public void onPause() {
        lifecycleSubject.onNext(FragmentEvent.PAUSE);
        super.onPause();
    }

    @Override
    public void onStop() {
        lifecycleSubject.onNext(FragmentEvent.STOP);
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        lifecycleSubject.onNext(FragmentEvent.DESTROY_VIEW);
        super.onDestroyView();
    }


    @Override
    public void onDetach() {
        lifecycleSubject.onNext(FragmentEvent.DETACH);
        super.onDetach();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        getActivity().overridePendingTransition(R.anim.right_in, R.anim.staty);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        getActivity().overridePendingTransition(R.anim.right_in, R.anim.staty);
    }
}
